﻿// ============================================================================
//  _ __   ___  ___  ___ _ __ ___  _   _
// | '_ \ / _ \/ __|/ _ \ '_ ` _ \| | | |
// | | | |  __/\__ \  __/ | | | | | |_| |
// |_| |_|\___||___/\___|_| |_| |_|\__,_|
//
// NES Emulator by daxnet, 2024
// MIT License
// ============================================================================

using System;
using System.IO;
using System.Windows.Forms;
using Krypton.Navigator;
using NesEmu.Explorer.Controls;
using NesEmu.Explorer.Properties;

namespace NesEmu.Explorer.Pages;

public partial class HexViewerPage : UserControl
{
    #region Public Constructors

    public HexViewerPage(FrmMain parent, FileBrowserPage.FileBrowserItemTag selectedNesFile)
        : this()
    {
        _parent = parent;
        _selectedNesFile = selectedNesFile;

        if (!string.IsNullOrEmpty(selectedNesFile.FilePath))
            _editor.FileName = selectedNesFile.FilePath ?? string.Empty;
    }

    #endregion Public Constructors

    #region Private Constructors

    private HexViewerPage()
    {
        InitializeComponent();
        elementHost.Child = _editor;
    }

    #endregion Private Constructors

    #region Public Classes

    public sealed class Page : KryptonPage
    {
        #region Public Fields

        public const string PageName = "E300ED6D-04E3-487A-9955-211FAF9AC82A";

        #endregion Public Fields

        #region Private Fields

        private readonly HexViewerPage _ctrl;

        #endregion Private Fields

        #region Public Constructors

        public Page(FrmMain parent, object? args = null)
        {
            if (args is not FileBrowserPage.FileBrowserItemTag selectedNesFile)
                throw new ArgumentException(@"Selected node doesn't have NES file information.", nameof(args));

            Text = @$"Binary - {Path.GetFileName(selectedNesFile.FilePath)}";
            TextTitle = Text;
            TextDescription = TextTitle;
            ImageSmall = Resources.Binary;

            _ctrl = new HexViewerPage(parent, selectedNesFile)
            {
                Dock = DockStyle.Fill
            };

            Controls.Add(_ctrl);
        }

        #endregion Public Constructors
    }

    #endregion Public Classes

    #region Private Fields

    private readonly NesHexEditor _editor = new();
    private readonly FrmMain? _parent;
    private readonly FileBrowserPage.FileBrowserItemTag? _selectedNesFile;

    #endregion Private Fields
}